package casts.misc
{
   import flash.display.MovieClip;
   import flash.events.Event;

   /**
    * 多語系元系範例
    * @author cjboy | cjboy1984@gmail.com
    * @usage
      // 換語系
      // LanguageBox.lang = 2;
      LanguageBox.changeTo(3);
    */
   public class LanguageBox extends MovieClip
   {
      // language option == frame
      public static var lang:uint = 1;

      private static var all:Array = new Array();

      public function LanguageBox()
      {
         stop();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // --------------------- LINE ---------------------

      public static function changeTo(_lang:uint):void
      {
         lang = _lang;

         for (var i:uint = 0 ; i < all.length; ++i)
         {
            var mc:MovieClip = all[i] as MovieClip;
            if (lang <= mc.totalFrames)
            {
               mc.gotoAndStop(lang);
            }
         }
      }

      // ################### protected ##################

      // #################### private ###################

      private function onAdd(e:Event):void
      {
         // add to singleton
         if (-1 == all.indexOf(this))
         {
            all.push(this);
         }

         // first time to set language
         if (lang <= totalFrames)
         {
            gotoAndStop(lang);
         }
      }

      private function onRemove(e:Event):void
      {
         // remove from singleton
         var ii:int = all.indexOf(this);
         if (-1 == ii)
         {
            all.splice(ii, 1);
         }
      }

      // --------------------- LINE ---------------------

   }

}